package com.yy.tools.load
{
	import com.yy.base.App;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	/**
	 * 加载通用组件 多线程加载
	 *加载服务器的swf到本地 
	 * @author YY
	 * 
	 */
	public class LoadSwfToLoacal
	{
		
		private var _progressCallBack:Function; //加载过程中回调
		private var _completeCallBack:Function; //加载完成回调
		private var _totalSize:int; //swf的大小
		private var _isLoadedSize:int; //已经加载的文件的大小  单位字节
		private var _swfInfos:Array;//swf 的地址
		private var _completeCount:int = 0; //已经加载完成的数目
		private var _loaderArr:Array = [];//保存loader的数组
		public function LoadSwfToLoacal()
		{
		}
		
		/**
		 * 开始同时加载多个swf
		 * @param address swf 的地址
		 * @param totalSize 文件的总大小
		 * @param progressCallBack 加载过程 回调
		 * @param completeCallBack 完成回调
		 * 
		 */
		public function loadSwf(swfInfos:Array, completeCallBack:Function = null, progressCallBack:Function = null):void {
			this._swfInfos = swfInfos;
			this._totalSize = getTotalSize(swfInfos);
			this._progressCallBack = progressCallBack;
			this._completeCallBack = completeCallBack;
			var i:int;
			var len:int = swfInfos.length;
			var load:MyLoader;
			for(i=0; i<len; i++) {
				load = new MyLoader();
				load.data.id = i;
				load.data.loaded = 0;
				load.data.swfInfo = swfInfos[i];
				_loaderArr.push(load);
				if(Boolean(progressCallBack)) {
					load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressFunc);
				}
				load.contentLoaderInfo.addEventListener(Event.COMPLETE, completeFunc);
				load.load(new URLRequest(LoadSwfParamInfo(swfInfos[i]).address), new LoaderContext(false, App.appDomain));
			}
		}
		
		/**
		 *计算要加载的多个swf的大小 
		 * @param arr
		 * @return 
		 * 
		 */
		private function getTotalSize(arr:Array):int {
			var sum:int = 0;
			var i:int;
			var len:int = arr.length;
			var info:LoadSwfParamInfo;
			for(i=0; i<len; i++) {
				info = arr[i];
				sum += info.swfSize;
			}
			return sum;
		}
		
		/**
		 *加载进程 
		 *  系统每调用这个方法一次 
		 *  这个方法会调用回调函数_progressCallBack
		 *  并传入 一个对象{totalBytes:(整形数据:总大小),bytesLoaded:(整形数据:已经加载的数据大小)}
		 * @param e
		 * 
		 */
		private function progressFunc(e:ProgressEvent):void {
			var data:Object = {};
			var ml:MyLoader = getLoaderByLoaderInfo(LoaderInfo(e.currentTarget));
			var raiseSize:int = ml.contentLoaderInfo.bytesLoaded - ml.data.loaded;//这次加载的增量
			_isLoadedSize += raiseSize;
			ml.data.loaded = ml.contentLoaderInfo.bytesLoaded;
			data.totalBytes = this._totalSize; //总字节数
			data.bytesLoaded = this._isLoadedSize;  //已经加载字节数
			this._progressCallBack(data);
		}
		
		/**
		 *通过loaderInfo 从头loaderArr中获取MyLoader对象 
		 * @return 
		 * 
		 */
		private function getLoaderByLoaderInfo(loadInfo:LoaderInfo):MyLoader {
			var i:int;
			var len:int = _loaderArr.length;
			for(i=0; i<len; i++) {
				if(loadInfo == _loaderArr[i].contentLoaderInfo) {
					return _loaderArr[i];
				}
			}
			return null;
		}
		
		/**
		 *加载完成 
		 * @param e
		 * 
		 */
		private function completeFunc(e:Event):void {
			var ml:MyLoader = getLoaderByLoaderInfo(LoaderInfo(e.currentTarget));
			ml.unload();
			if(++_completeCount == _swfInfos.length) {
				if(Boolean(_completeCallBack)) {
					_completeCallBack();
					dispose();
				}
			}
		}
		
		/**
		 *释放资源 
		 * 
		 */
		public function dispose():void {
			removeEventListener();
			_progressCallBack = null;
			_completeCallBack = null;
			_loaderArr = null;
			_swfInfos = null;
		}
		
		/**
		 *移除loaderInfo的侦听事件 
		 * 
		 */
		private function removeEventListener():void {
			var i:int;
			var len:int = _loaderArr.length;
			var loaderInfo:LoaderInfo;
			for(i=0; i<len; i++) {
				loaderInfo = _loaderArr[i].contentLoaderInfo;
				loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressFunc);
				loaderInfo.removeEventListener(Event.COMPLETE, completeFunc);
			}
		}
	}
}